﻿@model HQPad.Models.Skill

@{
    ViewBag.Title = "Edit: " + @Html.Raw(Model.Name);
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    List<HQPad.Models.Bonus> bonusList = ViewBag.BonusList as List<HQPad.Models.Bonus>;
    HQPad.Models.SkillBonusValueListModel skillBonusValueList = ViewBag.SkillBonusValueList as HQPad.Models.SkillBonusValueListModel;
}

<h1>Edit skill: @Html.Raw(Model.Name)</h1>

@section PageScripts {
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

    <script type="text/javascript">
        var skillBonuses = @Html.Raw(skillBonusValueList.ToJson());

        $(document).ready(function () {
            //$('.bonus-value-list').attr('disabled', 'disabled');

            $('.bonus-type-list').on('change', function () {
                var selected = $($(this).find('option:selected')[0]);

                var valueList = $('.bonus-value-list');

                valueList.empty();
                var listFilled = fillSkillBonusValueList(selected.val());

                if (listFilled) {
                    valueList.removeAttr('disabled');
                }
                else {
                    valueList.attr('disabled', 'disabled');
                }
            });

            $('.bonus-type-list').change(); // fire chnage event.
        });

        // function to fill the skill bonus value select list based on a key value.
        function fillSkillBonusValueList(key) {
            var filled = false;

            $.each(skillBonuses, function (index, value) {
                if (value.key == key) {
                    $('.bonus-value-list').append('<option value="' + value.value +'">' + value.name + '</option>');
                    filled = true;
                }
            });

            return filled;
        }
    </script>
}

<section class="content">

@using (Html.BeginForm())
{
    <fieldset class="block form form-wide-label">
        <legend>Basics</legend>

        @Html.ValidationSummary(true)

            <div class="field-item">
                <div class="label">
                    @Html.LabelFor(model => model.Name)
                </div>
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
            <div class="field-item">
                <div class="label">
                    @Html.LabelFor(model => model.LegacyName)
                </div>
                @Html.DisplayFor(model => model.LegacyName)
            </div>

            <div class="field-item">
                <div class="label">
                    @Html.LabelFor(model => model.Slug)
                </div>
                @Html.TextBoxFor(model => model.Slug, new { disabled = true })
                @Html.ValidationMessageFor(model => model.Slug)
            </div>

            <div class="field-item">
                <div class="label">
                    @Html.LabelFor(model => model.Deleted)
                </div>
                @Html.EditorFor(model => model.Deleted)
                @Html.ValidationMessageFor(model => model.Deleted)
            </div>

            <div class="field-item">
                <div class="label">
                    @Html.LabelFor(model => model.MaxRanks)
                </div>
                @Html.EditorFor(model => model.MaxRanks)
                @Html.ValidationMessageFor(model => model.MaxRanks)
            </div>

            <div class="field-item">
                <div class="label">
                    @Html.LabelFor(model => model.Status)
                </div>
                @Html.EditorFor(model => model.Status)
                @Html.ValidationMessageFor(model => model.Status)
            </div>

            <div class="field-item">
                <div class="label">
                    @Html.LabelFor(model => model.IsBasic)
                </div>
                @Html.EditorFor(model => model.IsBasic)
                @Html.ValidationMessageFor(model => model.IsBasic)
            </div>

            <div class="field-item">
                <div class="label">
                    @Html.LabelFor(model => model.TypeOfSkill)
                </div>
                @Html.DropDownList("TypeOfSkill")
                @Html.ValidationMessageFor(model => model.TypeOfSkill)
            </div>

        @Html.HiddenFor(model => model.RuleSetID)
        @Html.HiddenFor(model => model.SkillID)

        <div class="field-item">
            <table style="width: 100%">
                <tr>
                    <th>Table</th>
                    <th>Cost/constant</th>
                    <th>Factor</th>
                </tr>
        @{
    int cnt = 0;
    List<HQPad.Models.SkillCost> costs = ViewBag.Costs;

    foreach (var cost in costs)
    {
                @:<tr>
                    @: <td> 
                        @cost.Table.Race @:&nbsp;@cost.Table.Class
                    @:</td>
                    @: <td> 
                        <input type="text" 
                            @(Html.Raw("name=\"cost_" + @cost.SkillCostID + "\""))
                            value="@cost.Cost"
                            class="text-box-small" /> 
                    @:</td>
                    @: <td> 
                        <input type="text" 
                            @(Html.Raw("name=\"fac_" + @cost.SkillCostID + "\""))
                            value="@cost.Factor"
                            class="text-box-small" /> 
                    @:</td>                            
                @:</tr>
            }
        }
            </table>
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

    <fieldset class="block form form-wide-label">
        <legend>Bonuses</legend>
        <p>Each skill can have one or more bonuses.</p>
        <table>
            <thead>
                <tr>
                    <th>Type</th>
                    <th>Quantity</th>
                    <th>Value</th>
                    <th>Requirement</th>
                    <th></th>
                </tr>
            </thead>
        @{

            if (bonusList == null || bonusList.Count == 0)
            {
        @:<tr><td colspan="4">No bonuses for this skill</td></tr>
    }
            else
            {
                foreach (HQPad.Models.Bonus bonus in bonusList)
                {
            @:<tr>
                @:<td>@Html.Raw(bonus.Type)</td>
                @:<td>@Html.Raw(bonus.Quantity.ToString())</td>
                @:<td>@Html.Raw(!string.IsNullOrEmpty(bonus.Value) ? bonus.Value : "")</td>
                @:<td>@Html.Raw(!string.IsNullOrEmpty(bonus.Requirement) ? bonus.Requirement : "")</td>
                @:<td>@Html.ActionLink("Delete", "DeleteBonus", "Skill", new { id = bonus.BonusID }, null)</td>
            @:</tr>
        }
            }
         }
        </table>
    </fieldset>
    
    @using (Html.BeginForm("AddSkillBonus", "Skill", new { slug = Model.Slug }))
    {
        <fieldset class="block form form-wide-label">
            <legend>Add a bonus</legend>
            <div class="field-item">
            <div class="label"><label for="BonusTypes">Type</label></div>
            @Html.DropDownList("BonusTypes", null, new { @class = "bonus-type-list" })</div>
            
            <div class="field-item">
            <div class="label"><label for="quantity">Quantity (the number of times the bonues applies)</label></div>
            @Html.DropDownList("QuantityList")</div>   
            
            <div class="field-item">
            <div class="label"><label for="value">Value (an arbitary value, e.g. the level of spell slot known or a pipe delimited list of affected skill slugs)</label></div>
            <select name="value" class="bonus-value-list" ></select></div>   
         
            <div class="field-item">
            <div class="label"><label for="requirement">Requirement (an arbitrary requirement for the bonus to be allowed.)</label></div>
            @Html.DropDownList("requirement", new SelectListItem[] { new SelectListItem() { Text = "Armour must be equipped", Value = "EquippedArmour" } }, "None")
            </div>
            <p><input type="submit" value="Add Skill Bonus" /></p>
        </fieldset>
    }
    
    <fieldset class="block form form-wide-label">
        <legend>Requirements</legend>
        <p>For the skill to be available:
            <ul>
                <li>All AND skill requirements must be met. Any one of all OR skill requirements must be met.</li>
                <li>Any guild requirement can be met</li>
                <li>The highest rank requirement must be met</li>
            </ul>
        </p>
        <table >
            <thead>
                <tr>
                    <th>Combines with</th>
                    <th>Description</th>
                    <th></th>
                </tr>
            </thead>
        @{
            var requirements = (List<HQPad.Models.SkillRequirement>)ViewBag.Requirements;

            if (requirements == null || requirements.Count == 0)
            {
        @:<tr><td colspan="3">No requirements</td></tr>
    }
            else
            {
                foreach (HQPad.Models.SkillRequirement req in requirements)
                {
            @:<tr>
                @:<td>@Html.Raw(req.Combination)</td>
                @:<td>@Html.Raw(req.Description())</td>
                @:<td>@Html.ActionLink("Delete", "DeleteRequirement", "Skill", new { id = req.SkillRequirementID }, null)</td>
            @:</tr>
        }
            }
         }
        </table>
    </fieldset>

    @using (Html.BeginForm("AddSkillRequirement", "Skill", new { slug = Model.Slug }))
    {
        <fieldset class="block form form-wide-label">
            <legend>Add skill requirement</legend>
            
            <div class="field-item">
            <div class="label"><label for="SkillList">Select skill</label></div>
                @Html.DropDownList("SkillList")
            </div>

            <div class="field-item">
            <div class="label"><label for="combination">How does it combine?</label></div>
                @Html.DropDownList("combination", new SelectList(new[] { "AND", "OR" }), null, null)
            </div>

            <div class="field-item">
            <div class="label"><label for="quantity">Enter ranks required</label></div>
                @Html.TextBox("quantity", "1")
            </div>
            <input type="submit" value="Add Skill Requirement" />
        </fieldset>
    }

    @using (Html.BeginForm("AddGuildRequirement", "Skill", new { slug = Model.Slug }))
    {
        <fieldset class="block form form-wide-label">
            <legend>Add guild requirement</legend>

            <div class="field-item">
            <div class="label"><label for="GuildList">Select guild</label></div>
                @Html.DropDownList("GuildList")
            </div>

            <div class="field-item">
            <div class="label"><label for="guildLevel">At guild level</label></div>
                @Html.DropDownList("guildLevel", new SelectList(new[] { "1", "2", "3" }))
            </div>

            <div class="field-item">
            <div class="label"><label for="PrimaryGuildOnly">Primary guild only</label></div>
                @Html.CheckBox("PrimaryGuildOnly")
            </div>
            <input type="submit" value="Add Guild Requirement" />
        </fieldset>
    }

    @using (Html.BeginForm("AddRankRequirement", "Skill", new { slug = Model.Slug }))
    {
        <fieldset class="block form form-wide-label">
            <legend>Add minimum rank requirement</legend>

            <div class="field-item">
            <div class="label"><label for="minRank">Enter rank</label></div>
                @Html.TextBox("minRank", "64")
            </div>

            <div class="field-item">
            <div class="label"><label for="combination">How does it combine?</label></div>
                @Html.DropDownList("combination", new SelectList(new[] { "AND", "OR" }), null, null)
            </div>
            <input type="submit" value="Add Rank Requirement" />
        </fieldset>
    }




    <div class="block">
        @Html.ActionLink("Back to List", "Index", "Skill", new { id = ViewBag.RuleSetID }, new { @class = "button" })
    </div>
</section>